package com.mxchip.livestar.ui.network.wifimanager;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.List;

public class WifiManager extends BaseWifiManager {

    private WifiManager(Context context) {
        super(context);
    }

    public static IWifiManager create(Context context) {
        return new WifiManager(context);
    }

    @Override
    public boolean isOpened() {
        return manager.isWifiEnabled();
    }

    @Override
    public void openWifi() {
        if (!manager.isWifiEnabled())
            manager.setWifiEnabled(true);
    }

    @Override
    public void closeWifi() {
        if (manager.isWifiEnabled())
            manager.setWifiEnabled(false);
    }

    @Override
    public void scanWifi() {
        manager.startScan();
    }

    private static final String TAG = "WifiManager";

    @Override
    public boolean disConnectWifi(IWifi wifi) {
        boolean disconnect = false;
        boolean disable = false;
        List<WifiConfiguration> wifiConfigs = manager.getConfiguredNetworks();
        for (WifiConfiguration wifiConfig : wifiConfigs) {
            Log.e(TAG, "SSID = " + wifiConfig.SSID + " netId = " + String.valueOf(wifiConfig.networkId));
            if (wifiConfig.SSID.equals(wifi.SSID())) {
                Log.d(TAG, "disConnectWifi: :"+wifiConfig.SSID+" wifiConfig.networkId:"+wifiConfig.networkId);
//                disable = manager.disableNetwork(wifiConfig.networkId);
//                disconnect = manager.disconnect();
                forgetNetwork(manager,wifiConfig.networkId);
            }

        }


        scanWifi();
        return disable && disconnect;
    }
    // 忘记网络.
    public static void forgetNetwork(android.net.wifi.WifiManager manager, int networkId) {
        if (manager == null) {
            return;
        }
        try {
            Method forget = manager.getClass().getDeclaredMethod("forget", int.class, Class.forName("android.net.wifi.WifiManager$ActionListener"));
            if (forget != null) {
                forget.setAccessible(true);
                forget.invoke(manager, networkId, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public boolean connectEncryptWifi(IWifi wifi, String password) {
        if (manager.getConnectionInfo() != null && wifi.SSID().equals(manager.getConnectionInfo().getSSID()))
            return true;
        int networkId = WifiHelper.configOrCreateWifi(manager, wifi, password);
        boolean ret = manager.enableNetwork(networkId, true);
        modifyWifi(wifi.SSID(), "开始连接...");
        return ret;
    }

    @Override
    public boolean connectSavedWifi(IWifi wifi) {
        int networkId = WifiHelper.configOrCreateWifi(manager, wifi, null);
        boolean ret = manager.enableNetwork(networkId, true);
        modifyWifi(wifi.SSID(), "开始连接...");
        return ret;
    }

    @Override
    public boolean connectOpenWifi(IWifi wifi) {
        boolean ret = connectEncryptWifi(wifi, null);
        modifyWifi(wifi.SSID(), "开始连接...");
        return ret;
    }

    @Override
    public boolean removeWifi(IWifi wifi) {
        boolean ret = WifiHelper.deleteWifiConfiguration(manager, wifi);
        modifyWifi();
        return ret;
    }

    @Override
    public List<IWifi> getWifi() {
        return wifis;
    }


}
